package com.otarriinae.swagger;

import com.github.xiaoymin.knife4j.spring.filter.ProductionSecurityFilter;
import com.github.xiaoymin.knife4j.spring.filter.SecurityBasicAuthFilter;
import com.github.xiaoymin.knife4j.spring.model.MarkdownFiles;
import com.otarriinae.swagger.knife4j.Knife4jProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

/**
 * @author Wang Chengwei
 * @since 1.0
 */
@EnableConfigurationProperties({Knife4jProperties.class})
@ComponentScan(
        basePackages = {
                "com.github.xiaoymin.knife4j.spring.plugin",
                "com.github.xiaoymin.knife4j.spring.web"
        }
)
@Slf4j
@ConditionalOnWebApplication
public class OtarriinaeSwaggerAutoConfiguration {

    private final Environment environment;

    public OtarriinaeSwaggerAutoConfiguration(Environment environment) {
        this.environment = environment;
    }

    /**
     * 初始化自定义Markdown特性
     *
     * @param knife4jProperties 配置文件
     * @return markdownFiles
     */
    @ConditionalOnMissingBean(MarkdownFiles.class)
    @Bean(initMethod = "init")
    public MarkdownFiles markdownFiles(Knife4jProperties knife4jProperties) {
        MarkdownFiles markdownFiles;
        if (knife4jProperties == null) {
            markdownFiles = new MarkdownFiles(environment != null ? environment.getProperty("knife4j.markdowns") : "");
        } else {
            markdownFiles = new MarkdownFiles(knife4jProperties.getMarkdowns() == null ? "" : knife4jProperties.getMarkdowns());
        }
        return markdownFiles;
    }

    @Bean
    @ConditionalOnMissingBean(SecurityBasicAuthFilter.class)
    public SecurityBasicAuthFilter securityBasicAuthFilter(Knife4jProperties knife4jProperties) {
        String dftUserName = "admin";
        String dftPass = "123321";
        SecurityBasicAuthFilter securityBasicAuthFilter;
        if (knife4jProperties == null) {
            securityBasicAuthFilter = getSecurityBasicAuthFilterNotProperties(dftUserName, dftPass);
        } else {
            //判断非空
            if (knife4jProperties.getBasic() == null) {
                securityBasicAuthFilter = new SecurityBasicAuthFilter(false, dftUserName, dftPass);
            } else {
                securityBasicAuthFilter = new SecurityBasicAuthFilter(knife4jProperties.getBasic().isEnable(), knife4jProperties.getBasic().getUsername(), knife4jProperties.getBasic().getPassword());
            }
        }
        return securityBasicAuthFilter;
    }

    private SecurityBasicAuthFilter getSecurityBasicAuthFilterNotProperties(String dftUserName, String dftPass) {
        SecurityBasicAuthFilter securityBasicAuthFilter = null;
        if (environment != null) {
            String enableAuth = environment.getProperty("knife4j.basic.enable");
            boolean enableSwaggerBasicAuth = Boolean.parseBoolean(enableAuth);
            if (enableSwaggerBasicAuth) {
                //如果开启basic验证,从配置文件中获取用户名和密码
                String pUser = environment.getProperty("knife4j.basic.username");
                String pPass = environment.getProperty("knife4j.basic.password");
                if (!StringUtils.isEmpty(pUser)) {
                    dftUserName = pUser;
                }
                if (!StringUtils.isEmpty(pPass)) {
                    dftPass = pPass;
                }
            }
            securityBasicAuthFilter = new SecurityBasicAuthFilter(enableSwaggerBasicAuth, dftUserName, dftPass);
        }
        return securityBasicAuthFilter;
    }


    @Bean
    @ConditionalOnMissingBean(ProductionSecurityFilter.class)
    public ProductionSecurityFilter productionSecurityFilter(Knife4jProperties knife4jProperties) {
        boolean prod = false;
        ProductionSecurityFilter p;
        if (knife4jProperties == null) {
            if (environment != null) {
                String prodStr = environment.getProperty("knife4j.production");
                if (log.isDebugEnabled()) {
                    log.debug("swagger.production:{}", prodStr);
                }
                prod = Boolean.parseBoolean(prodStr);
            }
            p = new ProductionSecurityFilter(prod);
        } else {
            p = new ProductionSecurityFilter(knife4jProperties.isProduction());
        }

        return p;
    }
}
